package org.example.word.parser;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.example.word.AbstractWordDocumentParser;
import org.example.word.pojo.AdaptApplicationForm;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AdaptApplicationFormParser extends AbstractWordDocumentParser<AdaptApplicationForm> {
    
    @Override
    protected Map<String, String> getFieldMapping() {
        Map<String, String> mapping = new HashMap<>();
        mapping.put("厂家名称", "manufacturer");
        mapping.put("联系人", "contact");
        mapping.put("联系电话", "phone");
        mapping.put("填表日期", "date");
        mapping.put("联系邮箱", "email");
        mapping.put("设备名称/型号", "deviceModel");
        mapping.put("设备主控规格-CPU/MCU型号", "cpuModel");
        mapping.put("设备主控当前操作系统以及版本", "osVersion");
        mapping.put("设备主控规格-RAM大小", "ram");
        mapping.put("设备主控规格-ROM大小", "rom");
        mapping.put("通信模组芯片规格及型号", "commModuleSpec");
        mapping.put("通信模组", "commModule");
        mapping.put("设备类型", "deviceType");
        mapping.put("设备标准物资类别代号与名称（代号参考《电鸿适配开发测试指南》）", "resourceCode");
        mapping.put("产品专业领域", "productExpertise");
        mapping.put("适配模式及适配版本", "adaptModeDetail");
        return mapping;
    }
    
    @Override
    protected Class<AdaptApplicationForm> getResultType() {
        return AdaptApplicationForm.class;
    }
    
    @Override
    protected void processSpecialFields(XWPFDocument document, AdaptApplicationForm result) {
        for (XWPFTable table : document.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    String cellText = cell.getText().trim();
                    if (cellText.contains("：")) {
                        String[] parts = cellText.split("：");
                        if (parts.length >= 2) {
                            String label = parts[0].trim();
                            String value = parts[1].trim();
                            
                            switch (label) {
                                case "厂家联系人":
                                    result.setSignerManufacturer(value);
                                    break;
                                case "业务专业":
                                    result.setSignerBusiness(value);
                                    break;
                                case "电鸿适配中心":
                                    result.setSignerCenter(value);
                                    break;
                                case "数字化专业":
                                    result.setSignerDigital(value);
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }
} 